home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / clib02.arc / RMAIN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-10-18  |  4.3 KB  |  179 lines

  1. #define MSDOS2
  2.  
  3. #include "dos.h"
  4. #include "stdio.h"
  5. #include "ctype.h"
  6. #include "ios1.h"
  7.  
  8. #define MAXARG 100              /* maximum command line arguments */
  9.  
  10. extern int _stack,_fmode,_iomode;
  11. extern char _iname[],_oname[],_dos;
  12. extern struct UFB _ufbs[];
  13.  
  14. static int argc;            /* arg count */
  15. static char *argv[MAXARG];        /* arg pointers */
  16.  
  17.  
  18. /**
  19.  *
  20.  * name         _main - process command line, open files, and call "main"
  21.  *
  22.  * synopsis     _main(line);
  23.  *              char *line;     ptr to command line that caused execution
  24.  *
  25.  * description    This function performs the standard pre-processing for
  26.  *        the main module of a C program.  It accepts a command
  27.  *        line of the form
  28.  *
  29.  *            pgmname arg1 arg2 ...
  30.  *
  31.  *        and builds a list of pointers to each argument.  The first
  32.  *        pointer is to the program name.  For some environments, the
  33.  *        standard I/O files are also opened, using file names that
  34.  *        were set up by the OS interface module XCMAIN.
  35.  *
  36.  ********
  37.  *        Modified: 11 April 1984
  38.  *              Ross Nelson
  39.  *
  40.  *        This routine now treats quoted strings in the command line
  41.  *        as a single token, and allows the expansion of wildcard
  42.  *        characters (see WILDCARD.C).  A quoted string begins with
  43.  *        one of the characters ' or " and continues until that
  44.  *        character is found again (unless escaped by a \).  Any
  45.  *        command argument containing the MSDOS wildcard characters
  46.  *        (* and ?) must be quoted or this routine will attempt to
  47.  *        perform filename expansion on them.  The program will
  48.  *        exit if any wildcard expansion fails to match.    If wildcard
  49.  *        expansion fills available memory or overflows MAXARG, a
  50.  *        messaage will be printed, but processing will continue.
  51.  ********
  52.  *
  53.  *        Further mods: Oct-11-84 Chuck Forsberg V2.12, Unix pathnames
  54.  *
  55.  */
  56. _main(line)
  57. char *line;
  58. {
  59.     char c;
  60.     char quoted;
  61.     int x;
  62. #ifdef MSDOS2
  63.     char buf[135], *wild, *malloc();
  64. #endif
  65. #if UNIX == 0
  66.     FILE *fp0, *fp1, *fp2;
  67.     extern int _bufsiz;
  68.     char *getmem();
  69. #endif
  70.  
  71. /*
  72.  *
  73.  * Build argument pointer list
  74.  *
  75.  */
  76.     for(argc = 0; argc < MAXARG; )
  77.     {
  78.         while(isspace(*line))
  79.             line++;
  80.         if(*line == '\0')
  81.             break;
  82.         argv[argc++] = line;
  83.         quoted = 0;
  84.         if (((*line == '\'') || (*line == '"')) && (*(line-1) != '\\')) 
  85.         {
  86.             quoted++;
  87.             c = *line;
  88.             argv[argc-1]++;
  89.             while (*++line)
  90.                 if ((*line == c) && (*(line-1) != '\\'))
  91.                     break;
  92.         }
  93.         else
  94.             while((*line != '\0') && (isspace(*line) == 0))
  95.                 line++;
  96.         c = *line;
  97.         *line++ = '\0';
  98. #ifdef MSDOS2
  99.         if (!quoted && stpbrk(argv[argc-1], "*?")) {
  100.             wild = argv[--argc];
  101.             if (!wildcard(wild, 0x10, buf)) {
  102.                 bdos (9, "No match.\r\n$");
  103.                 exit (1);
  104.             }
  105.             else
  106.                 do {
  107.                     if (strcmp(buf, ".") && strcmp(buf, "..")) 
  108.                     {
  109.                         if ((argc == MAXARG) ||
  110.                         !(argv[argc] = malloc(strlen(buf)+1)))
  111.                         {
  112.                             bdos (9, "Wildcard expansion truncated.\r\n$");
  113.                             break;
  114.                         }
  115.                         strcpy(argv[argc++], buf);
  116.                     }
  117.                 }
  118.                     while (wildcard(wild, 0x10, buf));
  119.         }
  120. #endif
  121.         if(c == '\0')
  122.             break;
  123.     }
  124. /*
  125.  *
  126.  * Open standard files
  127.  *
  128.  */
  129. #if UNIX == 0
  130.     if(_dos < 2)
  131.     {
  132.         fp0 = freopen(_iname,"r",stdin);
  133.         if(_oname[0] != '>')
  134.             fp1 = freopen(_oname,"w",stdout);
  135.         else
  136.             fp1 = freopen(&_oname[1],"a",stdout);
  137.         fp2 = freopen("","a",stderr);
  138.         if (fp2 == NULL)
  139.             _exit(1);
  140.         if (fp0 == NULL) {
  141.             fputs("Can't open stdin file\n", fp2);
  142.             exit(1);
  143.         }
  144.         setbuf(fp0, getmem(_bufsiz));    /* set stdin buffered */
  145.         fp0->_flag &= ~_IOMYBUF;    /* allow rlsmem if later set unbuff'd */
  146.         if (fp1 == NULL) {
  147.             fputs("Can't open stdout file\n", fp2);
  148.             exit(1);
  149.         }
  150.     }
  151.     else {
  152.         stdin->_file = 0;
  153.         stdin->_flag = _IOREAD;
  154.         stdout->_file = 1;
  155.         stdout->_flag = _IOWRT;
  156.         stderr->_file = 2;
  157.         stderr->_flag = _IOWRT | _IONBF;
  158.  
  159.         x = ((_fmode ^ _iomode) & 0x8000) ? UFB_NT : 0;
  160.         _ufbs[0].ufbflg = UFB_OP | UFB_RA | x;
  161.         _ufbs[1].ufbfh = 1;
  162.         _ufbs[1].ufbflg = UFB_OP | UFB_WA | x;
  163.         _ufbs[2].ufbfh = 2;
  164.         _ufbs[2].ufbflg = UFB_OP | UFB_WA | x;
  165.         if (_fgdi(1) & 0x80)
  166.             stdout->_flag |= _IONBF;
  167.     }
  168. #endif
  169.  
  170.  
  171. /*
  172.  *
  173.  * Call the program already.
  174.  *
  175.  */
  176.     main(argc,argv);              /* Call main function */
  177.     exit(0);
  178. }
  179.